using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using FAQComponents;
using FAQComponents.API;
using FAQComponents.Objects;
using Adge.UI.WebControls;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for EmailAdmin.
	/// </summary>
	public partial class EmailAdmin : ClientSidePage
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			PermissionAPI.EnsureAdministrator();

			if (!Page.IsPostBack)
			{
				emailTemplates.DataSource = EmailAPI.GetAllEmailTemplates();
				emailTemplates.DataBind();

				emailTemplates_SelectedIndexChanged(null, EventArgs.Empty);
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void emailTemplates_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// Load the data for the selected EmailTemplate
			int emailTemplateID = InvariantString.ToInt32(emailTemplates.SelectedValue);
			EmailTemplate et = EmailAPI.GetEmailTemplate(emailTemplateID);

			subject.Text = et.Subject;
			template.Text = et.Template;
		}

		protected void btnSaveTemplate_Click(object sender, System.EventArgs e)
		{
			if (!Page.IsValid) return;

			EmailTemplate et = new EmailTemplate();
			et.EmailTemplateID = InvariantString.ToInt32(emailTemplates.SelectedValue);
			et.Subject = subject.Text;
			et.Template = template.Text;

			EmailAPI.UpdateEmailTemplate(et);

			base.DisplayClientSideAlert("The email template has been updated.");
		}
	}
}
